package com.huel.taobaostore;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.method.CharacterPickerDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.huel.fragment.Cart_Fragment;
import com.huel.fragment.Index_Fragment;
import com.huel.fragment.PhoneFragment;
import com.huel.fragment.login_Fragment;

public class MainActivity extends Activity {

    private Index_Fragment index_fragment;
    private login_Fragment login_fragment;
    private Cart_Fragment cart_fragment;
    private ImageView imb ,btn_cart,message_icon;
    private  ImageView first_page_icon;
    private PhoneFragment phoneFragment;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imb=findViewById(R.id.myfile_icon);
        btn_cart=findViewById(R.id.btn_cart);
        message_icon=findViewById(R.id.message_icon);
        first_page_icon=findViewById(R.id.first_page_icon);
        //activity 启动之后 准备开启首页
        getIndex();




        //插入一条数据 测试使用



        first_page_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //逻辑为 点击之后 跳转到 首页
                if (index_fragment == null) {
                    index_fragment = new Index_Fragment();
                }
                //创建FragmentManager对象
                FragmentManager manager = getFragmentManager();
                //创建FragmentTransaction事务对象
                FragmentTransaction fragmentTransaction = manager.beginTransaction();
                //使用replace（将要替换位置的i的，替换的页面）方法实现页面的替换
                //   所谓 替换 就是 先去移除 fragment 然后 再添加fragment  移除所有fragment



                fragmentTransaction.replace(R.id.fragment, index_fragment);



                //提交事务
                fragmentTransaction.commit();

                Toast.makeText(MainActivity.this,"切换成功",Toast.LENGTH_SHORT).show();
            }
        });
        imb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"我的淘宝",Toast.LENGTH_SHORT).show();

                //逻辑为 点击之后 跳转到 另一个 登录  fragment 然后实现登录
                if (login_fragment == null) {
                    login_fragment = new login_Fragment();

                }
                //创建FragmentManager对象
                FragmentManager manager = getFragmentManager();
                //创建FragmentTransaction事务对象
                FragmentTransaction fragmentTransaction = manager.beginTransaction();
                //使用replace（将要替换位置的i的，替换的页面）方法实现页面的替换
                //   所谓 替换 就是 先去移除 fragment 然后 再添加fragment  移除所有fragment
                Bundle bundle = new Bundle();

                fragmentTransaction.replace(R.id.fragment, login_fragment);
                //提交事
                fragmentTransaction.commit();
                Toast.makeText(MainActivity.this,"切换成功",Toast.LENGTH_SHORT).show();
            }
        });


        btn_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"我的购物车",Toast.LENGTH_SHORT).show();

                //逻辑为 点击之后 跳转到 另一个 登录  fragment 然后实现登录
                if (cart_fragment == null) {
                    cart_fragment = new Cart_Fragment();

                }
                //创建FragmentManager对象
                FragmentManager manager = getFragmentManager();
                //创建FragmentTransaction事务对象
                FragmentTransaction fragmentTransaction = manager.beginTransaction();
                //使用replace（将要替换位置的i的，替换的页面）方法实现页面的替换
                //   所谓 替换 就是 先去移除 fragment 然后 再添加fragment  移除所有fragment
                Bundle bundle = new Bundle();

                fragmentTransaction.replace(R.id.fragment, cart_fragment);
                //提交事
                fragmentTransaction.commit();
                Toast.makeText(MainActivity.this,"切换成功",Toast.LENGTH_SHORT).show();
            }
        });

        message_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPhone();
            }
        });




    }

    public void getIndex() {
        if (index_fragment == null ) {
            index_fragment = new Index_Fragment();

        }
        //创建FragmentManager对象
        FragmentManager manager = getFragmentManager();
        //创建FragmentTransaction事务对象
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        //使用replace（将要替换位置的i的，替换的页面）方法实现页面的替换

        //图片按钮点击之后 用 addinformationFragment 去替换页面

        //   所谓 替换 就是 先去移除 fragment 然后 再添加fragment  移除所有fragment

        fragmentTransaction.add(R.id.fragment, index_fragment);
        //提交事务

        fragmentTransaction.commit();

    }


    public void getPhone() {
        if (phoneFragment == null ) {
            phoneFragment = new PhoneFragment();
        }
        //创建FragmentManager对象
        FragmentManager manager = getFragmentManager();
        //创建FragmentTransaction事务对象
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment, phoneFragment);
        //提交事务
        fragmentTransaction.commit();
    }
}